//
//  YZAuthenticationVC.m
//  VET
//
//  Created by 刘文靖 on 2018/6/29.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZAuthenticationVC.h"
#import "UIViewController+YZImagePicker.h"
#import "YZAuthenticationAuditVC.h"
#import "YZGradientBtn.h"
#import "YZPersonalVM.h"
#import "YZUser.h"
@interface YZAuthenticationVC ()
@property (weak, nonatomic) IBOutlet UIButton *avatarButton;
@property (weak, nonatomic) IBOutlet YZGradientBtn *submitButton;
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tipLabelTopConstraint;
@property (strong, nonatomic) UIView *maskView;
@property (strong, nonatomic) YZPersonalVM *viewModel;
@end

@implementation YZAuthenticationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"专业认证";
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
    if (viewControllers.count > 2) {
        [viewControllers removeObject:self];
        self.navigationController.viewControllers = viewControllers;
    }
}

- (IBAction)avatarButtonClicked{
    [self choosePicture];
}

- (IBAction)submitButtonClicked {
    //1上传图片到后台
    [[self.viewModel uploadCertificate:self.avatarButton.currentBackgroundImage] subscribeNext:^(NSDictionary *json) {
        [YZUser sharedInstance].authStatus = 1;
    } completed:^{
        //2,上传成功后 跳转到审核中
        YZAuthenticationAuditVC *auditVC =  [[UIStoryboard storyboardWithName:@"personalCenter" bundle:nil] instantiateViewControllerWithIdentifier:@"YZAuthenticationAuditVC"];
        [self.navigationController pushViewController:auditVC animated:YES];
        [self.navigationController popViewControllerAnimated:NO];
    }];
}

#pragma mark - UIImagePickerController Delegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    UIImage *image = [self imageFromPickerVC:picker gotPicWithDicInfo:info];
    if (image) {
        [self.avatarButton setBackgroundImage:image forState:UIControlStateNormal];
        self.tipLabel.text = @"重新选择";
        self.tipLabel.textColor = kColorWithHex(0xffffff);
        self.tipLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:14];
        self.tipLabelTopConstraint.constant -= 10;
        self.maskView.backgroundColor = kColorWithHexAlpha(0x434343, 0.30);
        [self.view bringSubviewToFront:self.tipLabel];
        self.submitButton.enabled = YES;
    }
}

- (UIView *)maskView{
    if (!_maskView) {
        _maskView = [[UIView alloc] initWithFrame:self.avatarButton.frame];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarButtonClicked)];
        [_maskView addGestureRecognizer:tapGesture];
        [self.view addSubview:_maskView];
    }
    return _maskView;
}

- (YZPersonalVM *)viewModel{
    if (!_viewModel) {
        _viewModel = [[YZPersonalVM alloc] init];
    }
    return _viewModel;
}
@end
